package com.teng.smartdrawer.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.teng.smartdrawer.R;
import com.teng.smartdrawer.adapter.WordAdapter;
import com.teng.smartdrawer.bean.Seek;
import com.teng.smartdrawer.listener.SearchKeywordListener;
import com.teng.smartdrawer.utils.SeekComparator;
import com.teng.smartdrawer.utils.Utils;
import com.teng.smartdrawer.view.FullListView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;



/**
 * Created by teng on 16/2/23.
 */
public class SearchRecordFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private FullListView recordListView;
    private GridView recommendGridView;
    private TextView recent_search_text;
    private LinearLayout removeRecord;
    private View history_line;
    private List<Seek> seekList;
    private DbManager db = x.getDb(Utils.daoConfig);
    private WordAdapter recordAdapter;

    private SearchKeywordListener searchKeywordListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_search_record , null);

        recordListView = (FullListView) ret.findViewById(R.id.search_record_listview);
        recommendGridView = (GridView) ret.findViewById(R.id.search_recommend_gridview);
        history_line = ret.findViewById(R.id.history_line);

        removeRecord = (LinearLayout) ret.findViewById(R.id.foot_record_remove);
        removeRecord.setOnClickListener(this);
        recent_search_text = (TextView) ret.findViewById(R.id.recent_search_text);

        seekList = query();

        if (seekList.size() > 0) {
            showRecord(true);
            Collections.sort(seekList, new SeekComparator());
        }
        else {
            showRecord(false);
        }

        recordAdapter = new WordAdapter(getContext() , seekList);
        recordListView.setAdapter(recordAdapter);

        return ret;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        recordListView.setOnItemClickListener(this);

    }

    /**
     * 查找搜有的搜索记录
     */
    private List<Seek> query() {
        List<Seek> ret = null;
        try {
            ret = db.findAll(Seek.class);

           if (ret != null && ret.size() > 8){
               ret = ret.subList(0 , 7);
           }

            if (ret == null) {
                ret = new ArrayList<Seek>();
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 是否隐藏搜索记录
     */
    private void showRecord(boolean isShow){
        if (isShow){
            removeRecord.setVisibility(View.VISIBLE);
            recent_search_text.setVisibility(View.VISIBLE);
            recordListView.setVisibility(View.VISIBLE);
            history_line.setVisibility(View.VISIBLE);
        }else {
            removeRecord.setVisibility(View.GONE);
            recent_search_text.setVisibility(View.GONE);
            recordListView.setVisibility(View.GONE);
            history_line.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.foot_record_remove:
                try {

                    db.delete(Seek.class);
                    seekList.clear();
                    recordAdapter.notifyDataSetChanged();

                    showRecord(false);

                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;

            default:

                break;
        }
    }

    public SearchKeywordListener getSearchKeywordListener() {
        return searchKeywordListener;
    }

    public void setSearchKeywordListener(SearchKeywordListener searchKeywordListener) {
        this.searchKeywordListener = searchKeywordListener;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        searchKeywordListener.search(seekList.get(position).getName());
    }
}
